package cn.sail.spring.mvc.controller;

import cn.sail.spring.mvc.entity.Account;
import cn.sail.spring.mvc.vo.ResultVO;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

@Controller
@RequestMapping("account")
@Api(tags = "用户相关业务接口")
public class AccountController{

    @ResponseBody
    @RequestMapping(value = "login",method = RequestMethod.GET)
    @ApiOperation(value = "",hidden = true)
    public ResultVO login(String userName, String password){
        System.out.println("login password:" + password);
        System.out.println("login userName:" + userName);
        ResultVO resultVO = new ResultVO();
        resultVO.addData("info","login hello"+userName);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping(value = "login",method = RequestMethod.POST)
    @ApiOperation("登录接口")
    public ResultVO login(Account account, HttpServletRequest request, HttpServletResponse response){
        request.getSession().setAttribute("account",account);
        System.out.println(account);
        ResultVO resultVO = new ResultVO();
        resultVO.addData("account",account);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping(value = "loginWithJson",method = RequestMethod.POST)
    @ApiOperation("登录接口 json")
    public ResultVO loginWithJson(@RequestBody Account account, HttpServletRequest request, HttpServletResponse response){
        request.getSession().setAttribute("account",account);
        System.out.println(account);
        ResultVO resultVO = new ResultVO();
        resultVO.addData("account",account);
        return resultVO;
    }

    @ResponseBody
    @RequestMapping(value = "loginFail",method = RequestMethod.GET)
    @ApiOperation(value = "",hidden = true)
    public ResultVO loginWithJson(){
        throw new RuntimeException("密码错误");
    }

    @ResponseBody
    @RequestMapping(value = "getAccount",method = RequestMethod.GET)
    @ApiOperation("获取用户信息接口")
    public ResultVO getAccount(HttpServletRequest request, HttpServletResponse response){
        Account account = (Account) request.getSession().getAttribute("account");
        ResultVO resultVO = new ResultVO();
        resultVO.addData("account",account);
        return resultVO;
    }
}
